var myInsertIP = Components.utils.import("resource://insertip/insertip.jsm", null);
var CC = Components.classes;
var CI = Components.interfaces;

// When the options dialog is closed
function onOptionsOk() {
  // Delete the history in the places database.
  myInsertIP.deleteFilterHistory();
  // Submit usage stats if it's enabled.
  myInsertIP.submitStats();
}


function onOptionsLoad() {
  var prefs = myInsertIP.getPrefs('extensions.insertip.');

  if(prefs.getBoolPref('anonymousStats') == true) {
    document.getElementById('anonymous').checked = true;
  } else {
    document.getElementById('anonymous').checked = false;
  }
}


function addFilter() {
  var newFilter = document.getElementById('filter').value;

  if(newFilter == "" || newFilter == " ") {
    alert("You probably don't want to add a blank filter.");
    return;
  }

  if(newFilter.length < 5) {
    alert("That filter seems a bit short. Delete this filter if you're not sure what you're doing. You can find help with filters in the help menu.");
  }

  var badFilter = new RegExp(/(?:^\*|\*$)/);

  if(newFilter.match(badFilter)) {
    alert("You don't need to add * to a filter.");
    return;
  }

  var tree = document.getElementById('filterTree');

  var db = myFoxyHistory.getSettingsFileLocation();
  var dbConn = myFoxyHistory.openDatabaseConnection(db);

  dbConn.executeSimpleSQL('INSERT INTO filters VALUES(null, "true", "' + newFilter + '", 0)');

  tree.builder.rebuild();

  document.getElementById('filter').value = '';
}


function deleteFilter() {
  var tree = document.getElementById('filterTree');
  var index = tree.currentIndex;

  try {
    var filter = tree.view.getCellText(index, tree.columns.getNamedColumn('filterCol'));
  } catch(e) {
    dump("There was an error: " + e + "\n");
  }

  var db = myFoxyHistory.getSettingsFileLocation();
  var dbConn = myFoxyHistory.openDatabaseConnection(db);

  var query = 'DELETE FROM filters WHERE filter="' + filter + '"';

  try {
    dbConn.executeSimpleSQL(query);
  } catch(e) {
    dump("There was an error with the SQL: " + e + "\n");
  }

  tree.builder.rebuild();
}


// Toggle the anonymous stats preference.
function toggleAnonymousStatsPref() {
  var checkbox = document.getElementById('anonymous');
  var prefs = myFoxyHistory.getPrefs('extensions.foxyhistory.');

  if(checkbox.checked) {
    prefs.setBoolPref('anonymousStats', true);
  } else {
    prefs.setBoolPref('anonymousStats', false);
  }
}


// Open the FoxyHistory help page from the wiki
function openHelpPage() {
  var wm = CC["@mozilla.org/appshell/window-mediator;1"]
    .getService(CI.nsIWindowMediator);
  var mainWindow = wm.getMostRecentWindow("navigator:browser");
  mainWindow.getBrowser().selectedTab = mainWindow.getBrowser().addTab('http://essteeyou.com/w/FoxyHistory_Help');
}


// This whole function can be deleted when SQLite templates
// commit changes back to the database.
function onTreeDoubleClick(evt) {
  var tree = document.getElementById('filterTree');
  var tbo = tree.treeBoxObject;

  var row = { }, col = { }, child = { };
  tbo.getCellAt(evt.clientX, evt.clientY, row, col, child);

  var cellText = tree.view.getCellText(row.value, col.value);
  var newFilter = prompt("Edit the filter below", cellText);

  tree.view.setCellText(row.value, col.value, newFilter);

  var query = "UPDATE filters SET filter='" + newFilter + "' WHERE filter='" + cellText + "'";

  var dbConn = myFoxyHistory.getSettingsFileLocation();
  dbConn = myFoxyHistory.openDatabaseConnection(dbConn);

  try {
    dbConn.executeSimpleSQL(query);
  } catch(e) {
    dump("Error updating the filter: " + e + "\n");
  }
}
